package com.inifitness.common.event.service;

import jakarta.annotation.Resource;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.inifitness.common.event.publisher.EventPublisher;

import lombok.extern.log4j.Log4j2;
import org.slf4j.helpers.MessageFormatter;

/**
 * 事件服务基类 如果要用到事件发布，可以继承该类
 *
 * @author sunjinfeng
 * @version 1.0.0
 * @since 2024/01/11 12:52
 */
@Log4j2
public class EventServiceImpl<M extends BaseMapper<T>, T> extends ServiceImpl<M, T> {

  @Resource
  private EventPublisher publisher;

  public void publishEvent(boolean condition, String eventCode, Object params) {
    if (condition) {
      log.info(()-> MessageFormatter.format("发布事件:事件编码:{},事件参数:{}",  eventCode, params));
      log.info("发布事件:事件编码:{},事件参数:{}",  ()->eventCode, ()->params);
      publisher.publishEvent(eventCode, params);
    }
  }
}
